package me.w70.bot.wrappers;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;

import me.w70.bot.Bot;

public class RSInterface {
	private me.w70.hooks.Interface inter;

	private RSInterface parentInterface;

	private int index;

	public RSInterface(me.w70.hooks.Interface inter, RSInterface parentInterface) {
		this.parentInterface = parentInterface;
		this.inter = inter;
	}

	public boolean containsText(final String phrase) {
		if (getText().contains(phrase)) {
			return true;
		}
		return false;
	}

	/**
	 * Gets a child interface.
	 * 
	 * @param childID
	 * @return may return null if interface does not exist.
	 */
	public RSInterface getChild(int childID) {
		me.w70.hooks.Interface[] interfaces = inter.getChildren();
		if (childID < 0 || childID >= interfaces.length)
			return null;
		return new RSInterface(interfaces[childID], this);
	}

	public int getComponentID() {
		return inter.getComponentID();
	}

	public int getModelZoom() {
		return inter.getModelZoom();
	}

	public int[] getItemIDArray() {
		return inter.getItemIDArray();
	}

	public int[] getItemStackSizeArray() {
		return inter.getItemStackSizeArray();
	}

	public RSItem[] getItems() {
		List<RSItem> out = new LinkedList<RSItem>();
		int[] IDs = inter.getItemIDArray();
		int[] stacks = inter.getItemStackSizeArray();
		for (int i = 0; i < IDs.length; i++) {
			if (IDs[i] > 0)
				out.add(new RSItem(IDs[i], stacks[i]));
		}
		return out.toArray(new RSItem[1]);
	}

	public boolean hasParrent() {
		return parentInterface != null;
	}

	public RSInterface getParrent() {
		return parentInterface;
	}

	public int getX() {
		return inter.getX();
	}

	public int getY() {
		return inter.getY();
	}

	public int getMasterX() {
		return inter.getMasterX();
	}

	public int getMasterY() {
		return inter.getMasterY();
	}

	public int getAbsoluteX() {
		return getMasterX() + getX();
	}

	public int getAbsoluteY() {
		return getMasterY() + getY();
	}

	public int getHeight() {
		return inter.getHeight();
	}

	public int getWidth() {
		return inter.getWidth();
	}

	public Rectangle getArea() {
		return new Rectangle(getAbsoluteX(), getAbsoluteY(), getWidth(),
				getHeight());
	}

	public Point getCenter() {
		Rectangle area = getArea();
		return new Point((int) area.getCenterX(), (int) area.getCenterY());
	}

	/**
	 * Hack for interface debugger.
	 * 
	 * @param index
	 */
	public void setIndex(int index) {
		this.index = index;
	}

	/**
	 * Hack for interface debugger.
	 * 
	 * @return
	 */
	public String toString() {
		return "" + index;
		//return "interface[index=" + index + ",X=" + getAbsoluteX() + ",Y="
		//		+ getAbsoluteY() + ",text=" + getText() + "]";
	}

	public RSInterface[] getChildren() {
		if (inter != null) {
			List<RSInterface> out = new LinkedList<RSInterface>();
			me.w70.hooks.Interface[] interfaces = inter.getChildren();
			if (interfaces == null)
				return null;
			for (me.w70.hooks.Interface child : interfaces) {
				if (child == null)
					continue;
				out.add(new RSInterface(child, this));
			}
			return out.toArray(new RSInterface[1]);
		}
		return null;
	}

	public int getContainedItemID() {
		return inter.getContainedItemID();
	}

	public int getContainedItemStackSize() {
		return inter.getContainedItemStackSize();
	}

	private static Pattern pattern = Pattern.compile("\\<.+?\\>");

	public String getContainedItemName() {
		return pattern.matcher(inter.getContainedItemName()).replaceAll("");
	}

	public String[] getActions() {
		return inter.getActions();
	}

	public String getText() {
		return inter.getText();
	}

	public int getTextColor() {
		return inter.getTextColor();
	}

	public int getTextureID() {
		return inter.getTextureID();
	}

	public int getHorizontalScrollBarSize() {
		return inter.getHorizontalScrollBarSize();
	}

	public int getHorizontalScrollBarThumbPosition() {
		return inter.getHorizontalScrollBarThumbPosition();
	}

	public int getHorizontalScrollBarThumbSize() {
		return inter.getHorizontalScrollBarThumbSize();
	}

	public int getVerticalScrollBarThumbPosition() {
		return inter.getVerticalScrollBarThumbPosition();
	}

	public int getVerticalScrollBarThumbSize() {
		return inter.getVerticalScrollBarThumbSize();
	}

	public boolean textContains(String text) {
		String s = getText();
		return s != null && s.contains(text);
	}

	public boolean textContainsIgnoreCase(String text) {
		String s = getText();
		return s != null && s.toLowerCase().contains(text.toLowerCase());
	}

	public Point getRandomPointInside() {
		Rectangle rect = getArea();
		int randomX = Bot.methods.random(rect.x, rect.x + rect.width);
		int randomY = Bot.methods.random(rect.y, rect.y + rect.height);
		return new Point(randomX, randomY);
	}

	/**
	 * Gets the model id of the interface.
	 * 
	 * @return -1 if null.
	 */
	public int getModelID() {
		if (inter != null) {
			int modelID = inter.getSelectedModelID();
			if (modelID == -1) {
				modelID = inter.getNotSelectedModelID();
			}
			return modelID;
		}
		return -1;
	}

	/**
	 * Gets the model type of the interface
	 * 
	 * @return -1 if null
	 */
	public int getModelType() {
		if (inter != null) {
			int modelType = inter.getSelectedModelType();
			if (modelType == -1) {
				modelType = inter.getNotSelectedModelType();
			}
			return modelType;
		}
		return -1;
	}

	public void doClick() {
		Rectangle rect = getArea();
		int randomX = Bot.methods.random(rect.x, rect.x + rect.width);
		int randomY = Bot.methods.random(rect.y, rect.y + rect.height);
		Bot.methods.clickMouse(new Point(randomX, randomY), true);
	}

	public boolean doAction(String actionContains) {
		Rectangle rect = getArea();
		int randomX = Bot.methods.random(rect.x, rect.x + rect.width);
		int randomY = Bot.methods.random(rect.y, rect.y + rect.height);
		Bot.methods.moveMouse(new Point(randomX, randomY));
		return Bot.methods.atMenu(actionContains);
	}

	public boolean isValid() {
		return inter != null;
	}

}